package com.util;

import com.arcsoft.AFR_FSDK_FACEMODEL;
import com.arcsoft.ASVLOFFSCREEN;
import com.arcsoft.CLibrary;
import com.arcsoft.FaceInfo;
import com.face.client.model.AFREngine;
import com.face.client.model.FaceTestModel;
import com.face.client.model.NotFoundFaceException;
import com.face.client.model.Similarity;
import com.face.client.pojo.FeatureList;
import com.face.server.entity.FaceModel;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.ByteByReference;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/*
* AFR_FSDK_FACEMODEL
* 封装了对于虹软sdk的帮助方法
* 和float
* */
public class SDKUtils {

    public static Pointer createMem(int size) {
        return CLibrary.INSTANCE.malloc(size);
    }

    public static void free(Pointer memory) {
        CLibrary.INSTANCE.free(memory);
    }

    /**
     * 使用FD实现
     * @param asvloffscreen
     * @return
     */
    public static AFR_FSDK_FACEMODEL getModelFD(ASVLOFFSCREEN asvloffscreen) {
        FaceInfo[] faceInfoS = FaceUtils.getFaceByFD(asvloffscreen);
        return getModel(asvloffscreen, faceInfoS);
    }

    /**
     * 使用FT实现
     * @param asvloffscreen
     * @return
     */
    public static AFR_FSDK_FACEMODEL getModelFT(ASVLOFFSCREEN asvloffscreen) {
        FaceInfo[] faceInfoS = FaceUtils.getFaceByFT(asvloffscreen);
        return getModel(asvloffscreen, faceInfoS);
    }

    public static AFR_FSDK_FACEMODEL getModel(ASVLOFFSCREEN asvloffscreen, FaceInfo[] faceInfoS) {
        AFREngine afrEngine = new AFREngine();
        if (faceInfoS.length == 0) {
            return null;
        }
        return afrEngine.extractFRFeature(asvloffscreen, faceInfoS[0]);
    }


    public static AFR_FSDK_FACEMODEL getModelFD(String path) {
        ASVLOFFSCREEN asvloffscreen = ImageUtils.loadImage(path);
        return getModelFD(asvloffscreen);
    }




    public static float compareFace(AFR_FSDK_FACEMODEL modelA, AFR_FSDK_FACEMODEL modelB) {
        AFREngine afrEngine = new AFREngine();
        return afrEngine.faceMatching(modelA, modelB);
    }

    /*返回index*/

    public static int compare1ToN(AFR_FSDK_FACEMODEL model, List<FeatureList> modelList) {
        int len = modelList.size();
        double maxSimilar = -1;
        int maxIndex = -1;
        for (int i = 0; i < len; i++) {
            double sim = compareFace(model, modelList.get(i).getFacemodel());
            if (sim > maxSimilar) {
                maxSimilar = sim;
                maxIndex = i;
            }
        }
        System.out.println("相似度为 = " + maxSimilar);
        model.freeUnmanaged(); // 释放1 的一张图片
        return maxIndex;
    }



    /*下面是测试方法*/
    public static FaceModel getContentFromModel(AFR_FSDK_FACEMODEL model) {
        ByteByReference content = model.pbFeature;
        Pointer p = content.getPointer();
        int size = model.lFeatureSize;
        byte[] bytes = p.getByteArray(0, size);
        model.freeUnmanaged();
        FaceModel faceModel = new FaceModel();
        faceModel.setContent(bytes);
        faceModel.setSize(size);
        return faceModel;
    }



}
